/* Copyright (c) 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.b3log;

import com.google.gdata.client.projecthosting.IssuesQuery;
import com.google.gdata.client.projecthosting.ProjectHostingService;
import com.google.gdata.data.projecthosting.IssuesEntry;
import com.google.gdata.data.projecthosting.IssuesFeed;
import java.io.Console;
import java.net.URL;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Google Code Issues utilities.
 *
 * @author <a href="mailto:DL88250@gmail.com">Liang Ding</a>
 * @version 1.0.0.2, Oct 11, 2011
 */
public final class Issues {

    private static final String SOLO_VERSION;
    private static final String PROJECT;
    private static final String USER_NAME;
    private static final String PROXY_HOST;
    private static final String PROXY_PORT;

    static {
        PROJECT = ResourceBundle.getBundle("issues").getString("project");
        SOLO_VERSION = ResourceBundle.getBundle("issues").getString("version");
        USER_NAME = ResourceBundle.getBundle("issues").getString("username");

        System.out.println("Project: " + PROJECT);
        System.out.println("Version: " + SOLO_VERSION);

        PROXY_HOST = ResourceBundle.getBundle("issues").getString("proxy.host");
        PROXY_PORT = ResourceBundle.getBundle("issues").getString("proxy.port");

        if (null != PROXY_HOST && !"".equals(PROXY_HOST)
            && null != PROXY_PORT && !"".equals(PROXY_PORT)) {
            System.out.println("Configured proxy[host=" + PROXY_HOST + ", port=" + PROXY_PORT + "]");

            System.getProperties().put("proxySet", "true");
            System.getProperties().put("proxyHost", "127.0.0.1");
            System.getProperties().put("proxyPort", "8087");
        }
    }

    public static void main(final String[] arg) throws Exception {
        final Console cons = System.console();
        System.out.print("Please input your password: ");
        final String password = new String(cons.readPassword());

        Runtime.getRuntime().exec("ipconfig /flushdns");

        System.out.println("Authenticating....");
        final ProjectHostingService projectHostingService =
                new ProjectHostingService(PROJECT);
        projectHostingService.setUserCredentials(USER_NAME, password);

//        getProject(project, projectHostingService);
        getIssues(projectHostingService, PROJECT, SOLO_VERSION);
    }

    public static void getIssues(
            final ProjectHostingService projectHostingService,
            final String project,
            final String release) throws Exception {
        System.out.println("Retriving issues....");
        final URL feedURL = new URL("http://code.google.com/feeds/issues/p/"
                                    + project + "/issues/full");

        final IssuesQuery issuesQuery = new IssuesQuery(feedURL);
        issuesQuery.setMaxResults(200);

        issuesQuery.setLabel("Release-" + release);

        final IssuesFeed issuesFeed =
                projectHostingService.getFeed(issuesQuery, IssuesFeed.class);
        final List<IssuesEntry> issuesEntries = issuesFeed.getEntries();
        Collections.sort(issuesEntries, new Comparator<IssuesEntry>() {

            @Override
            public int compare(final IssuesEntry issuesEntry1,
                               final IssuesEntry issuesEntry2) {
                return issuesEntry1.getIssueId().getValue().
                        compareTo(issuesEntry2.getIssueId().getValue());
            }
        });

        int total = 0;
        final StringBuilder sb = new StringBuilder("<ul>");
        for (final IssuesEntry issuesEntry : issuesEntries) {
            sb.append("<li><a href=\"");
            sb.append(issuesEntry.getHtmlLink().getHref());
            sb.append("\">");

            sb.append(issuesEntry.getIssueId().getValue());
            sb.append(" ");

            sb.append(issuesEntry.getTitle().getPlainText());
            sb.append("</a></li>");
            sb.append("\r\n");

            total++;
        }
        sb.append("</ul>");
        System.out.println(sb.toString());

        System.out.println(total + " issues totally.");
    }
//    private static void getProject(final String project,
//                                   final ProjectHostingService projectHostingService)
//            throws Exception {
//        final URL feedURL = new URL("http://code.google.com/feeds/issues/p/"
//                                    + project + "/issues/full");
//        final ProjectsQuery projectsQuery = new ProjectsQuery(feedURL);
//        projectsQuery.setMaxResults(1000);
//        
//        final ProjectsFeed projectsFeed =
//                projectHostingService.getFeed(projectsQuery, ProjectsFeed.class);
//        
//        final List<ProjectSummaryEntry> projectSummaryEntrys =
//                projectsFeed.getEntries();
//        for (final ProjectSummaryEntry projectSummaryEntry :
//             projectSummaryEntrys) {
//            final List<Person> members = projectSummaryEntry.getAuthors();
//            for (final Person member : members) {
//                System.out.println(member.getName());
//            }
//        }
//    }
}
